package com.project2.controller;

import com.project2.anno.SaveLog;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.CartAddBo;
import com.project2.bean.bo.CartCheckedBo;
import com.project2.bean.bo.CartCheckoutBo;
import com.project2.bean.po.*;
import com.project2.bean.vo.CartIndexVo;
import com.project2.mapper.MarketCartMapper;
import com.project2.service.WxCartService;
import org.omg.CORBA.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * TODO
 *
 * @param
 * @author wj
 * @return
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;

    @RequestMapping("goodscount")
    public ResultVo goodsCount(){
        int count=wxCartService.goodsCount();
        return ResultVo.ok(count);
    }
    @RequestMapping("index")
    public ResultVo index(HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user==null){
            return ResultVo.ok("请登录",501);
        }
        CartIndexVo cartIndexVo=wxCartService.index(user);
        return ResultVo.ok(cartIndexVo);
    }
    @SaveLog(value = "小程序用户加购商品")
    @RequestMapping("add")
    public ResultVo add(@RequestBody CartAddBo cartAddBo, HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user==null){
            return ResultVo.ok("请登录",501);
        }
        int add = wxCartService.add(cartAddBo, user,false);
        if (add>0){
            int i = wxCartService.goodsCount();
            return ResultVo.ok(i);
        }else if(add==-1){
            return ResultVo.wrong("库存不足！");
        } else {
            return ResultVo.wrong("加入购物车失败！");
        }
    }
    @RequestMapping("checked")
    public ResultVo checked(@RequestBody CartCheckedBo cartCheckedBo){
        CartIndexVo cartIndexVo=wxCartService.checked(cartCheckedBo);
        return ResultVo.ok(cartIndexVo);
    }
    @RequestMapping("delete")
    public ResultVo deleted(@RequestBody CartCheckedBo cartCheckedBo,HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user==null){
            return ResultVo.ok("请登录",501);
        }
        CartIndexVo cartIndexVo=wxCartService.deleted(cartCheckedBo,user);
        if (cartIndexVo==null){
            return ResultVo.wrong("操作失败");
        }
        return ResultVo.ok(cartIndexVo);
    }
    @RequestMapping("update")
    public ResultVo update(@RequestBody CartAddBo cartAddBo){
        int i=wxCartService.update(cartAddBo);
        if (i<1){
            return ResultVo.wrong("更新失败");
        }
        return ResultVo.ok();
    }
    @RequestMapping("checkout")
    public ResultVo checkout(CartCheckoutBo cartCheckoutBo, HttpServletRequest request){
        //获得用户对象
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user==null){
            return ResultVo.ok("请登录",501);
        }
        //通过用户对象获得addressId与地址信息
        CartCheckoutVo cartCheckoutVo=wxCartService.checkout(cartCheckoutBo,user);
        if (cartCheckoutVo==null){
            return ResultVo.ok("库存不足",711);
        }
        return ResultVo.ok(cartCheckoutVo);
    }
    @RequestMapping("fastadd")
    public ResultVo fastAdd(@RequestBody CartAddBo cartAddBo,HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user==null){
            return ResultVo.ok("请登录",501);
        }
        int add = wxCartService.add(cartAddBo, user,true);
        if (add>0){
            //int i = wxCartService.goodsCount();
            return ResultVo.ok(add);
        }else if(add==-1){
            return ResultVo.wrong("库存不足！");
        }else {
            return ResultVo.wrong("加入购物车失败！");
        }
    }

}
